<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Website_Report extends CI_Controller {
	public function index(){
		#$this->session->userdata('user_id').'|'.$this->session->userdata('user_name');
		$this->layout->setLayout('default');
		$this->layout->setTitle('Website | Report');
		
		$sqlsms = "Select website_name From tdx_website Where 1=1 And sms_code = '" . $_GET['web'] . "'";
		$webQuery = $this->db->query($sqlsms);
		if ($webQuery->num_rows() > 0){
			$data['sitename'] = $webQuery->row(0)->website_name;
		}
		
		$month = isset($_GET['month']) ? date('Y').'-'.str_pad(quotes_to_entities($_GET['month']), 2, "0", STR_PAD_LEFT) : date('Y-m');
		$year = isset($_GET['year']) ? $_GET['year'] : date('Y');
		if ($year == "") $year = date('Y');
		$month = isset($_GET['month']) ? $year . '-'.str_pad(quotes_to_entities($_GET['month']), 2, "0", STR_PAD_LEFT) : date('Y-m');
				
		$arrDate = explode("-", $month);
		$data['month'] = $arrDate[1].'-'.$arrDate[0];
		//Tinh so ngay trong thang
		$day = cal_days_in_month(CAL_GREGORIAN, $arrDate[1],$arrDate[0]);
		$total = 0;
		$arrData = array();
		if(isset($_GET['web'])){
			$web = quotes_to_entities($_GET['web']);
			// Use Procedure in Phpmyadmin ======================================
			$con = mysql_connect($this->db->hostname, $this->db->username, $this->db->password, false,65536);
			mysql_select_db($this->db->database, $con);
			
			$sql = "call spSubTotalMonthSite('".$month."-01 00:00:00','".$month."-".$day." 23:59:59','".$web."');";
			$queryUser1 = mysql_query($sql);
			
			if ($queryUser1 === FALSE) {
				die(mysql_error());
			}
			
			while($row = mysql_fetch_array($queryUser1))
			{				
				$data['item'][] = $row;
				$arrData[$row['earn_date']] += intval($row['earm_day']);
				$total += intval($row['earm_day']);
			}
			
			mysql_close($con);
			// ==================================================================
		}
		
		if(isset($_GET['date'])){
			$today = $_GET['date'];			
			$strDate = 'Ngày '.$_GET['date'];
			// Use Procedure in Phpmyadmin ======================================
			$con = mysql_connect($this->db->hostname, $this->db->username, $this->db->password, false,65536);
			mysql_select_db($this->db->database, $con);
			$sql1 = "call spSubTotalMonth('".$today." 00:00:00','".$today." 23:59:59','".$filter."');";
			$queryUser1 = mysql_query($sql1);
			if ($queryUser1 === FALSE) {
				die(mysql_error());
				$data['earn_today'] = 0;
			}
			
			while($row = mysql_fetch_array($queryUser1))
			{				
				$data['earn_today'] += intval($row['earm_day']);
			}
			
			mysql_close($con);
			// ==================================================================		
		}
		else{
			$strDate = 'Hôm nay ('.date('d-m-Y').')';// .$pos.'--'.$sqlThisMonthEarning;
			$data['earn_today'] = intval($arrData[date('d-m-Y')]);
		}
		if ($web == "GN mkh198542776"){			
			if ($year != "2012") {
				$data['earm_site'] = "Thực nhận (Tổng APK + JAVA)";
				$data['earn_this_month_site'] = ($total*0.5);	
			}else{
				$data['earm_site'] = "Thực nhận (JAVA)";
				$data['earn_this_month_site'] = ($total*0.7);	
			}
			
		}else if ($web == "GN mkh198542776apk"){
			$data['earm_site'] = "Thực nhận";
			$data['earn_this_month_site'] = ($total*1);	
		}else if ($web == "GN mkh198542782"){
			$data['earm_site'] = "Thực nhận - mức 4200";
			$data['earn_this_month_site'] = ($total / 4000) * 4200;	
		}else{
			$data['earm_site'] = "Thực nhận";
			$data['earn_this_month_site'] = $total;	
		}
		$data['earn_yesterday'] = intval($arrData[date('d-m-Y', strtotime('-1 day'))]);
		$data['earn_this_month'] = $total;
		$data['str_date'] = $strDate;
		$data['month_data'] = $arrData;
		$data['currentUser'] = $this->session->userdata('user_name');
		$this->layout->view(THEME_DIR.'/website_report_index', $data);
	}
}